package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.commodity.convert.CategoryPropertyRelationshipConvert;
import com.jiangjiang.eshop.commodity.dao.CategoryPropertyRelationshipMapper;
import com.jiangjiang.eshop.commodity.domain.CategoryPropertyRelationshipEntity;
import com.jiangjiang.eshop.commodity.dto.CategoryPropertyRelationshipDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName CategoryPropertyRelationshipService
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/20 16:08
 * @Version 1.0
 **/
@Service
public class CategoryPropertyRelationshipService extends
        ServiceImpl<CategoryPropertyRelationshipMapper, CategoryPropertyRelationshipEntity> {

    @Autowired
    private CategoryPropertyRelationshipConvert categoryPropertyRelationshipConvert;

    /**
     * 批量保存类目和属性关联关系
     * @param relationshipDTOList
     */
    public void saveBatch(List<CategoryPropertyRelationshipDTO> relationshipDTOList) {
        if(CollectionUtils.isEmpty(relationshipDTOList)) {return;}
        this.saveBatch(categoryPropertyRelationshipConvert.convertDTOList2Entities(relationshipDTOList));
    }

    /**
     * 删除类目与属性的关联关系
     * @param categoryId
     */
    public void removeByCategoryId(Long categoryId) {
        LambdaQueryWrapper<CategoryPropertyRelationshipEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CategoryPropertyRelationshipEntity::getCategoryId, categoryId);
        this.remove(wrapper);
    }

    /**
     * 根据类目id查询类目和属性关联关系
     * @param categoryId 类目id
     */
    public List<CategoryPropertyRelationshipDTO> listByCategoryId(Long categoryId){
        LambdaQueryWrapper<CategoryPropertyRelationshipEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CategoryPropertyRelationshipEntity::getCategoryId, categoryId);
        return categoryPropertyRelationshipConvert.convertEntities2DTOList(this.list(wrapper));
    }

}
